package cn.lzj.basic.service.impl;

import cn.lzj.basic.mapper.BaseMapper;
import cn.lzj.basic.query.BaseQuery;
import cn.lzj.basic.service.IBaseService;
import cn.lzj.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private  BaseMapper<T> mapper;

    /**
     * 公共类的crud,分页查询,高级查询的业务层,部门业务层只需要写无限极树(通过上下部门的路径找到上级部门)的业务
     * @param t
     */
    @Override
    public void add(T t) {
        mapper.save(t);
    }

    @Override
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public void remove(Long id) {
        mapper.delete(id);
    }

    @Override
    public T queryById(Long id) {
        return  mapper.loadById(id);
    }

    @Override
    public List<T> queryAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //查询页面数据的总条数
        Long tatal = mapper.queryCount(query);
        //查询当前页的数据
        List<T> rows = mapper.queryData(query);
        return new PageList<T>(tatal, rows);

    }

    /**
     * 批量删除的方法抽取
     * @param ids
     */
    @Transactional
    @Override
    public void removeList(List<Long> ids) {
            mapper.removeList(ids);
    }
}
